package com.data4.order.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.data4.feignapi.client.SupplierClient;
import com.data4.feignapi.common.constants.OrderConstants;
import com.data4.feignapi.dto.OrderDTO;
import com.data4.feignapi.dto.PageDTO;
import com.data4.feignapi.dto.ProductPageDTO;
import com.data4.feignapi.dto.Result;
import com.data4.feignapi.entity.DOrder;
import com.data4.feignapi.entity.Product;
import com.data4.feignapi.vo.OrderByTimeVO;
import com.data4.feignapi.vo.OrderDetailVO;
import com.data4.feignapi.vo.ProductDetailVO;
import com.data4.order.mapper.DOrderMapper;
import com.data4.order.service.DOrderService;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Forever
 * @since 2024-12-11
 */
@Service
public class DOrderServiceImpl extends ServiceImpl<DOrderMapper, DOrder> implements DOrderService {
    @Resource
    private SupplierClient supplierClient;

    @Override
    public Result addOrder(OrderDTO orderDTO, Integer userId) {
        DOrder order = BeanUtil.toBean(orderDTO, DOrder.class);
        order.setUserId(userId);
        Integer marketPrice = supplierClient
                .getProductById(order.getProductId())
                .getMarketPrice();
        order.setTotalMoney(marketPrice * order.getProductNum());
        save(order);
        return Result.ok();
    }

    @Override
    public Result getOrderDetail(Integer orderId, Integer userId) {
        DOrder order = lambdaQuery()
                .eq(DOrder::getOrderId, orderId)
                .eq(DOrder::getUserId, userId)
                .one();
        if (BeanUtil.isEmpty(order)) return Result.fail("订单不存在");
        ProductDetailVO productDetail = supplierClient
                .getProductDetail(order.getProductId());
        OrderDetailVO orderDetailVO = BeanUtil.toBean(order, OrderDetailVO.class);
        orderDetailVO.setSingleMoney(productDetail.getMarketPrice());
        orderDetailVO.setProduct(productDetail);
        return Result.ok(orderDetailVO);
    }

    @Override
    public Result getOrderByTime(Long current, Long num, Integer userId) {
        Page<DOrder> orderPage = lambdaQuery()
                .eq(DOrder::getUserId, userId)
                .orderByDesc(DOrder::getOrderTime)
                .page(new Page<>(current, num));
        List<DOrder> orderList = orderPage
                .getRecords();
        List<OrderByTimeVO> orderByTimeVOS = orderList
                .stream()
                .map(this::getOrderByTimeVO)
                .collect(Collectors.toList());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setList(orderByTimeVOS);
        pageDTO.setTotalNum(orderPage.getTotal());
        return Result.ok(pageDTO);
    }

    @Override
    public Result getOrderByProductName(Integer productCurrent, Integer productPageNum,
                                        String productName, Integer userId) {
        ProductPageDTO productByNameLike = supplierClient
                .getProductByNameLike(productName, productCurrent, productPageNum);
        if (CollUtil.isEmpty(productByNameLike.getList())) return Result
                .fail("没有找到该商品的相关订单");
        List<Integer> productIds = productByNameLike
                .getList()
                .stream()
                .map(product -> product.getProductId())
                .collect(Collectors.toList());
        List<DOrder> orderList = lambdaQuery()
                .eq(DOrder::getUserId, userId)
                .in(DOrder::getProductId, productIds)
                .orderByDesc(DOrder::getOrderTime)
                .list();
        List<OrderByTimeVO> orderByTimeVOS = orderList
                .stream()
                .map(this::getOrderByTimeVO)
                .collect(Collectors.toList());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setList(orderByTimeVOS);
        pageDTO.setTotalNum(productByNameLike.getTotalNum());
        return Result.ok(pageDTO);
    }

    @Override
    public Result getProductGroup(List<String> groupIds, Integer userId) {
        QueryWrapper<DOrder> queryWrapper = new QueryWrapper<>();
        String query = StrUtil.join(",", groupIds);
        query = query + "," + OrderConstants.QUERY_GROUP;
        queryWrapper
                .eq("user_id", userId)
                .select(query)
                .groupBy(groupIds);
        List<Map<String, Object>> orderGroupMaps = listMaps(queryWrapper);
        return Result.ok(orderGroupMaps);
    }

    @Override
    public Result getOrderByPriceScope(Integer current, Integer num,
                                       Integer little, Integer big, Integer userId) {
        Page<DOrder> orderPage = lambdaQuery()
                .eq(DOrder::getUserId, userId)
                .ge(little.intValue() != -1, DOrder::getTotalMoney, little*100)
                .le(big.intValue() != -1, DOrder::getTotalMoney, big*100)
                .page(new Page<>(current, num));
        List<OrderByTimeVO> orderByTimeVOS = orderPage
                .getRecords()
                .stream()
                .map(this::getOrderByTimeVO)
                .toList();
        PageDTO pageDTO = new PageDTO();
        pageDTO.setList(orderByTimeVOS);
        pageDTO.setTotalNum(orderPage.getTotal());
        return Result.ok(pageDTO);
    }

    @NotNull
    private OrderByTimeVO getOrderByTimeVO(@NotNull DOrder DOrder) {
        Product product = supplierClient
                .getProductById(DOrder.getProductId());
        OrderByTimeVO orderByTimeVO = BeanUtil
                .toBean(DOrder, OrderByTimeVO.class);
        orderByTimeVO.setSingleMoney(product.getMarketPrice());
        orderByTimeVO.setProductName(product.getProductName());
        orderByTimeVO.setProductImg(product.getProductImg());
        orderByTimeVO.setProductDescription(product.getProductDescription());
        return orderByTimeVO;
    }
}
